import requests
from flask import request, Blueprint, render_template

from zz import zz_code, zz_redis, zz_dingtalk, zz_return, zz_log

log = zz_log.of()


def of():
    return Siyoumi()


class Siyoumi:
    def __init__(self):
        self.root = "https://app.lai-m.com/"

    def get_api_url(self, url_fix) -> str:
        return self.root + url_fix

    def find_cmd(self):
        """
        获取是否需要执行的命令
        """
        api_url = self.get_api_url("app/sh/sh_api/cmd")
        res = requests.get(api_url)

        log.info(f"url: {api_url}")
        log.info(f"return_str: {res.content}")
        return zz_return.parse(res.content)

    def set_run(self, task_id):
        """
        标记命令运行中
        @param task_id: 任务ID
        """
        api_url = self.get_api_url(f"app/sh/sh_api/cmd_run?id={task_id}")
        res = requests.get(api_url)

        log.info(f"url: {api_url}")
        log.info(f"return_str: {res.content}")
        return zz_return.parse(res.content)

    def set_done(self, task_id, return_msg=""):
        """
        标记已完成
        @param task_id: 任务ID
        @param return_msg: 执行反馈文本
        """
        form_data = {
            "id": task_id,
            "return_msg": return_msg,
        }
        api_url = self.get_api_url(f"app/sh/sh_api/cmd_done?id={task_id}")
        res = requests.post(api_url, data=form_data)

        log.info(f"url: {api_url}")
        log.info(f"return_str: {res.content}")
        return zz_return.parse(res.content)
